package com.sad.dao;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.sad.model.JsonObject;
import com.sad.model.Wish;

public abstract class BaseRecord<T> implements IRecord<T> {

	protected String getUrl(Map<String, Object> parms) {
		return "";
	}

	protected String postUrl(Map<String, Object> parms) {
		return "";
	}

	protected RequestBuilder builderParameter(RequestBuilder builder, Map<String, Object> parms) {
		return builder;
	};

	protected abstract JsonObject<T> parserJsonObject(String json) throws JsonParseException,
			JsonMappingException, IOException;

	public JsonObject post(Map<String, Object> parms) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// HttpPost httpPost = new HttpPost(postUrl(parms));
			// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			// setNameValuePair(nvps,parms);
			//
			// httpPost.setEntity(new UrlEncodedFormEntity(nvps));
			// CloseableHttpResponse response = httpclient.execute(httpPost);
			//
			//
			// System.out.println(response.getStatusLine());
			//
			// HttpEntity entity = response.getEntity();
			// // EntityUtils.consume(entity);
			//
			// String json = EntityUtils.toString(entity);
			// System.out.println(json);
			//
			// ObjectMapper mapper = new ObjectMapper();
			// JsonObject<Wish> jsonObject = mapper.readValue(json,
			// new TypeReference<JsonObject<Wish>>() {
			// });
			//
			// response.close();

//			HttpUriRequest login = RequestBuilder.post().setUri(new URI(this.postUrl(parms)))
//					.addParameter("IDToken1", "username").addParameter("IDToken2", "password")
//					.build();

			RequestBuilder builder = RequestBuilder.post().setUri(new URI(this.postUrl(parms)));
			builder = this.builderParameter(builder,parms);
			
			
			CloseableHttpResponse response2 = httpclient.execute(builder.build());
			HttpEntity entity = response2.getEntity();

			System.out.println("Login form get: " + response2.getStatusLine());
			// EntityUtils.consume(entity);
			String json = EntityUtils.toString(entity);
			ObjectMapper mapper = new ObjectMapper();
			JsonObject<Wish> jsonObject = mapper.readValue(json,
					new TypeReference<JsonObject<Wish>>() {
					});

			return jsonObject;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public JsonObject<T> get(Map<String, Object> parms) {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			String url = this.getUrl(parms);
			HttpGet httpGet = new HttpGet(url);

			System.out.println(url);

			CloseableHttpResponse response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String json = EntityUtils.toString(entity);

			System.out.println(json);

			// ObjectMapper mapper = new ObjectMapper();
			// JsonObject<T> jsonObject = mapper.readValue(json, new
			// TypeReference<JsonObject<T>>() {
			// });

			response.close();

			return this.parserJsonObject(json);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}
}
